﻿using ERP.ERPSystem.ProductionSystem.Dto;
using ERP.Team.ProductionSystem;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace ERP.ERPSystem.ProductionSystem
{
    /// <summary>
    /// 生产系统-排班管理-排班计划
    /// </summary>
    public class ShiftplanningService : ApplicationService, IShiftplanningService
    {
        private readonly IRepository<ShiftplanningModel, int> _shiftplanningRepository;

        public ShiftplanningService(IRepository<ShiftplanningModel, int> shiftplanningRepository)
        {
            _shiftplanningRepository = shiftplanningRepository;
        }

        /// <summary>
        /// 获取排班计划分页列表
        /// </summary>
        /// <param name="query">查询参数</param>
        /// <returns></returns>
        public async Task<ShiftplanningPageResultDTO> GetShiftplanningPageList(ShiftplanningQueryDTO query)
        {
            try
            {
                var queryable = await _shiftplanningRepository.GetQueryableAsync();

                // 构建查询条件
                var filteredQuery = queryable
                    .WhereIf(!string.IsNullOrEmpty(query.PlanNumber), m => m.PlanNumber.Contains(query.PlanNumber))
                    .WhereIf(!string.IsNullOrEmpty(query.PlanName), m => m.PlanName.Contains(query.PlanName))
                    .WhereIf(!string.IsNullOrEmpty(query.TeamType), m => m.TeamType.Contains(query.TeamType))
                    .WhereIf(!string.IsNullOrEmpty(query.StartTime), m => m.StartTime.Date >= DateTime.Parse(query.StartTime).Date)
                    .WhereIf(!string.IsNullOrEmpty(query.EndTime), m => m.EndTime.Date <= DateTime.Parse(query.EndTime).Date)
                    .OrderByDescending(m => m.Id);

                // 获取总数
                var totalCount = filteredQuery.Count();

                // 分页查询
                var pagedData = filteredQuery
                    .Skip((query.Page - 1) * query.Limit)
                    .Take(query.Limit)
                    .ToList();

                // 转换为DTO
                var dtoData = pagedData.Select(m => new ShiftplanningDTO
                {
                    Id = m.Id,
                    PlanNumber = m.PlanNumber,
                    PlanName = m.PlanName,
                    StartTime = m.StartTime,
                    EndTime = m.EndTime,
                    TeamType = m.TeamType,
                    ShiftWay = m.ShiftWay,
                    ProductionWay = m.ProductionWay,
                    DocumentStatus = m.DocumentStatus,
                    Descr = m.Descr
                }).ToList();

                return new ShiftplanningPageResultDTO
                {
                    Code = 0,
                    Msg = "success",
                    Count = totalCount,
                    Data = dtoData
                };
            }
            catch (Exception ex)
            {
                return new ShiftplanningPageResultDTO
                {
                    Code = 1,
                    Msg = $"查询失败：{ex.Message}",
                    Count = 0,
                    Data = new List<ShiftplanningDTO>()
                };
            }
        }

        /// <summary>
        /// 添加排班计划
        /// </summary>
        /// <param name="shiftplanningDto">排班计划数据</param>
        /// <returns></returns>
        public async Task<bool> AddShiftplanning(ShiftplanningDTO shiftplanningDto)
        {
            try
            {
                var shiftplanning = new ShiftplanningModel
                {
                    PlanNumber = shiftplanningDto.PlanNumber,
                    PlanName = shiftplanningDto.PlanName,
                    StartTime = shiftplanningDto.StartTime,
                    EndTime = shiftplanningDto.EndTime,
                    TeamType = shiftplanningDto.TeamType,
                    ShiftWay = shiftplanningDto.ShiftWay,
                    ProductionWay = shiftplanningDto.ProductionWay,
                    DocumentStatus = shiftplanningDto.DocumentStatus,
                    Descr = shiftplanningDto.Descr
                };

                await _shiftplanningRepository.InsertAsync(shiftplanning);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 获取排班计划详情
        /// </summary>
        /// <param name="id">排班计划ID</param>
        /// <returns></returns>
        public async Task<ShiftplanningDTO> GetShiftplanningById(int id)
        {
            try
            {
                var entity = await _shiftplanningRepository.GetAsync(id);
                if (entity == null)
                {
                    return null;
                }

                return new ShiftplanningDTO
                {
                    Id = entity.Id,
                    PlanNumber = entity.PlanNumber,
                    PlanName = entity.PlanName,
                    StartTime = entity.StartTime,
                    EndTime = entity.EndTime,
                    TeamType = entity.TeamType,
                    ShiftWay = entity.ShiftWay,
                    ProductionWay = entity.ProductionWay,
                    DocumentStatus = entity.DocumentStatus,
                    Descr = entity.Descr
                };
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 更新排班计划
        /// </summary>
        /// <param name="shiftplanningDto">排班计划数据</param>
        /// <returns></returns>
        public async Task<bool> UpdateShiftplanning(ShiftplanningDTO shiftplanningDto)
        {
            try
            {
                var entity = await _shiftplanningRepository.GetAsync(shiftplanningDto.Id);
                if (entity == null)
                {
                    return false;
                }

                // 更新实体属性
                entity.PlanNumber = shiftplanningDto.PlanNumber;
                entity.PlanName = shiftplanningDto.PlanName;
                entity.StartTime = shiftplanningDto.StartTime;
                entity.EndTime = shiftplanningDto.EndTime;
                entity.TeamType = shiftplanningDto.TeamType;
                entity.ShiftWay = shiftplanningDto.ShiftWay;
                entity.ProductionWay = shiftplanningDto.ProductionWay;
                entity.DocumentStatus = shiftplanningDto.DocumentStatus;
                entity.Descr = shiftplanningDto.Descr;

                await _shiftplanningRepository.UpdateAsync(entity);
                return true;
            }
            catch
            {
                return false;
            }
        }




    }
}
